package com.czl.module_user.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.UserInfo
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class UserViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    var userName = ObservableField("")
    var avatarUrl = ObservableField("")
    var userPhone = ObservableField("")
    val point = ObservableField("0")

    class UiChangeEvent {

    }

    val onNavSettingClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_SETTING)
    })
    val onNavIntegralClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_INTEGRAL_DETAIL)
    })
    val onNoticeCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Notice.F_NOTICE)
    })
    val onRepairReportClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Report.F_REPAIR_REPORT)
    })
    val onComplaintClick:BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Complaint.F_COMPLAINT_RECORD)
    })
    val onNavMyScoreClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_MY_SCORE)
    })
    val onNavPayRecordsClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Park.F_PAY_RECORDS)
    })
    val onMyActivityClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Activities.F_ACTIVITIES_MY)
    })
    val onNavPeopleManagerClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_PEOPLE_MANAGER)
    })

    val onDevelopingClick: BindingCommand<Void> = BindingCommand(BindingAction {
        showNormalToast("功能正在开发中，敬请期待")
    })

    /**
     * 获取用户详细信息
     */
    fun getUserInfo() {
        model.apply {
            getUserInfoNet()
                .compose(RxThreadHelper.rxSchedulerHelper(this@UserViewModel))
                .subscribe(object : ApiSubscriberHelper<BaseBean<UserInfo>>() {
                    override fun onResult(t: BaseBean<UserInfo>) {
                        t.data?.let {
                            saveUserInfoData(it)
                            userName.set(it.name)
                            userPhone.set(it.phone)
                            avatarUrl.set(it.avatarUrl)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showNormalToast(msg)
                    }

                })
        }
    }

    fun getPointDetail() {
        model.getPointDetail(
            model.getAreaId()
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<String>>() {
                override fun onResult(t: BaseBean<String>) {
                    if (t.code == 200) {
                        point.set(t.data)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }

            })
    }

}